<?php
class BaseManager extends Controller
{
	public function __construct(&$controller, &$action)
	{
		parent::__construct($controller, $action);
		
		Template::reset();
		Template::layout('manager', 'layout.html');
		
		fw::lang('manager');
	}
	
	# Configuration
	public function config()
	{
		$form = new Form('config');
		
		if($data = $form->validate())
		{
			foreach($data as $key=>$value)
			{
				if($key == 'config') continue;
				
				fw::$config[$key] = $value;
				
				db::query('UPDATE `'.DB_PREFIX.'config` SET `value`="'.db::escape($value).'" WHERE `key`="'.$key.'"');
			}
		}
		
		foreach(fw::$config as $key=>$value)
		{
			if(method_exists($this, $method = 'config_'.$key))
			{
				$this->$method($form, $value);
			} else
			{
				$form->field($key, 'text', array(), $value);
			}
		}
		
		require Template::render('manager', 'config.html');
	}
	
	private function config_version()
	{
	}
	
	private function config_lang($form, $value)
	{
		$options = array();
		
		$directory = opendir(APP.'lang');
		
		while(($folder = readdir($directory)) !== false)
		{
			if($folder != '.' && $folder != '..') $options[$folder] = $folder;
		}
		
		$form->field('lang', 'select', $options, $value);
	}
	
	private function config_style($form, $value)
	{
		$options = array();
		
		$directory = opendir(APP.'style');
		
		while(($folder = readdir($directory)) !== false)
		{
			if($folder != '.' && $folder != '..') $options[$folder] = $folder;
		}
		
		$form->field('style', 'select', $options, $value);
	}
}